﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace NHBTest
{
    public class Cat
    {
        private string id;
        private string name;
        private char sex;
        private float weight;

        public Cat()
        {
        }

        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual char Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public virtual float Weight
        {
            get { return weight; }
            set { weight = value; }
        }
    }

    public class TestCases
    {
        //private static readonly ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

        public static void TestSave()
        {
            try
            {
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
                ISession session = sessionFactory.OpenSession();
                ITransaction tx = session.BeginTransaction();
                Cat princess = new Cat();
                princess.Name = "Wusong";
                princess.Sex = 'M';
                princess.Weight = 1.4f;
                princess.Id = "101";
                session.Save(princess);
                session.Flush();
                tx.Commit();
                session.Close();
            }
            catch (Exception ex)
            {
            }
        }

        public static Cat TestQuery()
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            return session.Get<Cat>("9999");
        }

        public static Cat TestQuery(string id)
        {
            ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();
            return session.Load<Cat>(id);
        }

        public static IList<Cat> TestQueryAll()
        {
            try
            {
                ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
                ISession session = sessionFactory.OpenSession();
                ICriteria cr = session.CreateCriteria<Cat>();
                return cr.List<Cat>();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }

    }

}
